/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import controllers.entity.Admin;
import controllers.entity.ChamThi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HoaDuyen
 */
public class AdminDAO {
     private static Connection con = null;
    private static java.sql.PreparedStatement prst = null;
    public static List <Admin> getAllAdmin(){
        List <Admin> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM admin");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                String usename = rs.getString("usename");
                String password = rs.getString("password");
                Admin temp = new Admin(usename,password);
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public static boolean UpdateAdmin(Admin X,String name){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("UPDATE Admin SET usename = ?, password = ? WHERE usename = ?");
            prst.setString(1,X.getUseName());
            prst.setString(2,X.getPassWord());
            prst.setString(3,name);
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    
}
